***Growth variables

global root "C:\Users\chupilkm\EBRD\OCE - Maxim\War data\Earthquakes replication"

foreach var in "gdppc" {

clear
use "$root\Data\Working data\full_earthquakes.dta" 
tsset iso_code2 year
gen z = 1 if earthquake == 1 | year_pre_earthquake_1 == 1| year_pre_earthquake_2 == 1| year_pre_earthquake_3 == 1| year_pre_earthquake_4 == 1| year_pre_earthquake_5 == 1 | year_post_earthquake_1 == 1| year_post_earthquake_2 == 1| year_post_earthquake_3 == 1| year_post_earthquake_4 == 1| year_post_earthquake_5 == 1

sort iso_code year
gen `var'_growth = `var'/`var'[_n-1]*100-100 if iso_code[_n-1] == iso_code & year[_n-1] == year - 1

drop if `var'_growth == .
drop if gdppc == .
drop if pop == .
drop if populationdensity == .

preserve
sort iso_code2 year
by iso_code2: drop if year!=year[_n-1]+1 & iso_code2 == iso_code2[_n-1]
gen GDP_pc_growth_clean = `var'_growth if year_pre_earthquake_5 == 1 |year_pre_earthquake_4 == 1 |year_pre_earthquake_3 == 1 |year_pre_earthquake_2 == 1 |year_pre_earthquake_1 == 1 | earthquake == 1 | year_post_earthquake_1 == 1| year_post_earthquake_2 == 1| year_post_earthquake_3 == 1| year_post_earthquake_4 == 1| year_post_earthquake_5 == 1
drop if GDP_pc_growth_clean == .
egen a = rowtotal( year_pre_earthquake_1 year_pre_earthquake_2 year_pre_earthquake_3 year_pre_earthquake_4 year_pre_earthquake_5 year_post_earthquake_1 year_post_earthquake_2 year_post_earthquake_3 year_post_earthquake_4 year_post_earthquake_5 earthquake)
drop if a > 1
drop a
sort iso_code2 year

keep if earthquake == 1 & year_pre_earthquake_1[_n-1] == 1 & year[_n-1] == year-1

gen n = 1 if year != year[_n-1]+1| iso_code != iso_code[_n-1]
keep if n == 1
replace n = n[_n-1] + 1 if n[_n-1]!=.
sum n if earthquake[_n+1] == .
local number = cond(r(min)==r(max),r(min),.)
save "$root\Data\Working data\single earthquake episodes.dta", replace
keep iso_code iso_code2
duplicates drop
save "$root\Data\Working data\iso_conversion.dta", replace
restore

sort iso_code2 year

merge 1:1 iso_code year using "$root\Data\Working data\single earthquake episodes.dta"
drop _merge



cd "$root\Data\Working data\Synth"

forvalues i = 1(1)`number'{
preserve

forvalues s = 1(1)5 {
	replace n = n[_n+`s'] if year_pre_earthquake_`s' ==1 &  n[_n+`s'] == `i'
}

replace n =n[_n-1] if earthquake == 1 & n==. & n[_n-1] == `i'

forvalues s = 1(1)5 {
	replace n = n[_n-`s'] if year_post_earthquake_`s' ==1 & n[_n-`s'] == `i'
}


keep if n == `i' | z!= 1

sum year if n == `i' & n[_n-1] == .
local year_start = cond(r(min)==r(max),r(min),.)

sum year if n == `i' & n[_n+1] == .
local year_end = cond(r(min)==r(max),r(min),.)

sum year if earthquake == 1 & earthquake[_n-1] == 0
local earthquake_start = cond(r(min)==r(max),r(min),.)

sum year if earthquake == 1 & earthquake[_n+1] == 0
local earthquake_end = cond(r(min)==r(max),r(min),.)

sum iso_code2 if earthquake == 1 & earthquake[_n-1] == 0
local country = cond(r(min)==r(max),r(min),.)

sort earthquake
local iso = iso_code[1]

sort iso_code2 year

keep if year >= `year_start' & year <= `year_end'
sort iso_code2 year
capture noisily by iso_code2 : gen nyear=[_N]
capture noisily keep if nyear == `year_end' - `year_start' + 1
local year1 = `earthquake_start' - 1
local year2 = `earthquake_start' - 2
local year3 = `earthquake_start' - 3
tsset iso_code2 year
capture noisily synth `var'_growth gdppc pop populationdensity `var'_growth(`year1') `var'_growth(`year2') `var'_growth(`year3') , trunit(`country') trperiod(`earthquake_start') keep ("earthquake_`var'_`i'.dta", replace)
if _rc == 0 {

clear
capture noisily use "$root\Data\Working data\Synth\earthquake_`var'_`i'.dta"
capture noisily save "$root\Data\Working data\Synth\earthquake_`var'_`iso'_`earthquake_start'.dta", replace
gen iso_code2 = `country'
gen earthquake_start = `earthquake_start' 
gen earthquake_end = `earthquake_end' 
merge m:1 iso_code2 using "$root\Data\Working data\iso_conversion.dta"
drop if _merge == 2
drop _merge

capture noisily gen growth_earthquake = _Y_treated if _time  == `earthquake_start'

capture noisily gen comp_earthquake = _Y_synthetic if _time  == `earthquake_start'

capture noisily forvalues b = 1(1)5 {
	gen growth_pre_earthquake_`b' = _Y_treated if _time == `earthquake_start' - `b'
	gen growth_post_earthquake_`b' = _Y_treated if _time == `earthquake_end' + `b'
	gen comp_pre_earthquake_`b' = _Y_synthetic if _time == `earthquake_start' - `b'
	gen comp_post_earthquake_`b' = _Y_synthetic if _time == `earthquake_end' + `b'
}

capture noisily drop _Co_Number _W_Weight _Y_treated _Y_synthetic iso_code2 _time
capture noisily collapse growth_earthquake comp_earthquake growth_pre_earthquake_1 growth_post_earthquake_1 comp_pre_earthquake_1 comp_post_earthquake_1 growth_pre_earthquake_2 growth_post_earthquake_2 comp_pre_earthquake_2 comp_post_earthquake_2 growth_pre_earthquake_3 growth_post_earthquake_3 comp_pre_earthquake_3 comp_post_earthquake_3 growth_pre_earthquake_4 growth_post_earthquake_4 comp_pre_earthquake_4 comp_post_earthquake_4 growth_pre_earthquake_5 growth_post_earthquake_5 comp_pre_earthquake_5 comp_post_earthquake_5, by(iso_code earthquake_start earthquake_end)
capture noisily order iso_code earthquake_start earthquake_end growth_pre_earthquake_5 growth_pre_earthquake_4 growth_pre_earthquake_3 growth_pre_earthquake_2 growth_pre_earthquake_1 growth_earthquake growth_post_earthquake_1 growth_post_earthquake_2 growth_post_earthquake_3 growth_post_earthquake_4 growth_post_earthquake_5 comp_pre_earthquake_5 comp_pre_earthquake_4 comp_pre_earthquake_3 comp_pre_earthquake_2 comp_pre_earthquake_1 comp_earthquake comp_post_earthquake_1 comp_post_earthquake_2 comp_post_earthquake_3 comp_post_earthquake_4 comp_post_earthquake_5
save "$root\Data\Working data\Synth\earthquake_`var'_processed_`i'.dta", replace
}
restore
}

clear
forvalues i = 1(1)`number'{
capture noisily append using "$root\Data\Working data\Synth\earthquake_`var'_processed_`i'.dta"
}
drop if iso_code == "VNM" & earthquake_start == 1970
save "$root\Data\Working data\earthquake_`var'_dataset.dta", replace
}

***Share variables

foreach var in "debt_to_GDP" "csh_i" "csh_g"  "export_growth" "import_growth" {

clear
use "$root\Data\Working data\full_earthquakes.dta" 
tsset iso_code2 year
gen z = 1 if earthquake == 1 | year_pre_earthquake_1 == 1| year_pre_earthquake_2 == 1| year_pre_earthquake_3 == 1| year_pre_earthquake_4 == 1| year_pre_earthquake_5 == 1 | year_post_earthquake_1 == 1| year_post_earthquake_2 == 1| year_post_earthquake_3 == 1| year_post_earthquake_4 == 1| year_post_earthquake_5 == 1

sort iso_code year

drop if `var' == .
drop if gdppc == .
drop if pop == .
drop if populationdensity == .

preserve
sort iso_code2 year
by iso_code2: drop if year!=year[_n-1]+1 & iso_code2 == iso_code2[_n-1]
gen GDP_pc_growth_clean = `var' if year_pre_earthquake_5 == 1 |year_pre_earthquake_4 == 1 |year_pre_earthquake_3 == 1 |year_pre_earthquake_2 == 1 |year_pre_earthquake_1 == 1 | earthquake == 1 | year_post_earthquake_1 == 1| year_post_earthquake_2 == 1| year_post_earthquake_3 == 1| year_post_earthquake_4 == 1| year_post_earthquake_5 == 1
drop if GDP_pc_growth_clean == .
egen a = rowtotal( year_pre_earthquake_1 year_pre_earthquake_2 year_pre_earthquake_3 year_pre_earthquake_4 year_pre_earthquake_5 year_post_earthquake_1 year_post_earthquake_2 year_post_earthquake_3 year_post_earthquake_4 year_post_earthquake_5 earthquake)
drop if a > 1
drop a
sort iso_code2 year

keep if earthquake == 1 & year_pre_earthquake_1[_n-1] == 1 & year[_n-1] == year-1

gen n = 1 if year != year[_n-1]+1| iso_code != iso_code[_n-1]
keep if n == 1
replace n = n[_n-1] + 1 if n[_n-1]!=.
sum n if earthquake[_n+1] == .
local number = cond(r(min)==r(max),r(min),.)
save "$root\Data\Working data\single earthquake episodes.dta", replace
keep iso_code iso_code2
duplicates drop
save "$root\Data\Working data\iso_conversion.dta", replace
restore

sort iso_code2 year

merge 1:1 iso_code year using "$root\Data\Working data\single earthquake episodes.dta"
drop _merge


cd "$root\Data\Working data\Synth"

forvalues i = 1(1)`number'{
preserve

forvalues s = 1(1)5 {
	replace n = n[_n+`s'] if year_pre_earthquake_`s' ==1 &  n[_n+`s'] == `i'
}

replace n =n[_n-1] if earthquake == 1 & n==. & n[_n-1] == `i'

forvalues s = 1(1)5 {
	replace n = n[_n-`s'] if year_post_earthquake_`s' ==1 & n[_n-`s'] == `i'
}

keep if n == `i' | z!= 1

sum year if n == `i' & n[_n-1] == .
local year_start = cond(r(min)==r(max),r(min),.)

sum year if n == `i' & n[_n+1] == .
local year_end = cond(r(min)==r(max),r(min),.)

sum year if earthquake == 1 & earthquake[_n-1] == 0
local earthquake_start = cond(r(min)==r(max),r(min),.)

sum year if earthquake == 1 & earthquake[_n+1] == 0
local earthquake_end = cond(r(min)==r(max),r(min),.)

sum iso_code2 if earthquake == 1 & earthquake[_n-1] == 0
local country = cond(r(min)==r(max),r(min),.)

sort earthquake
local iso = iso_code[1]

sort iso_code2 year

keep if year >= `year_start' & year <= `year_end'
sort iso_code2 year
capture noisily by iso_code2 : gen nyear=[_N]
capture noisily keep if nyear == `year_end' - `year_start' + 1
local year1 = `earthquake_start' - 1
local year2 = `earthquake_start' - 2
local year3 = `earthquake_start' - 3
tsset iso_code2 year
capture noisily synth `var' gdppc pop populationdensity `var'(`year1') `var'(`year2') `var'(`year3') , trunit(`country') trperiod(`earthquake_start') keep ("earthquake_`var'_`i'.dta", replace)
if _rc == 0 {
clear
capture noisily use "$root\Data\Working data\Synth\earthquake_`var'_`i'.dta"
capture noisily save "$root\Data\Working data\Synth\earthquake_`var'_`iso'_`earthquake_start'.dta", replace
gen iso_code2 = `country'
gen earthquake_start = `earthquake_start' 
gen earthquake_end = `earthquake_end' 
merge m:1 iso_code2 using "$root\Data\Working data\iso_conversion.dta"
drop if _merge == 2
drop _merge

capture noisily gen growth_earthquake = _Y_treated if _time  == `earthquake_start'

capture noisily gen comp_earthquake = _Y_synthetic if _time  == `earthquake_start'

capture noisily forvalues b = 1(1)5 {
	gen growth_pre_earthquake_`b' = _Y_treated if _time == `earthquake_start' - `b'
	gen growth_post_earthquake_`b' = _Y_treated if _time == `earthquake_end' + `b'
	gen comp_pre_earthquake_`b' = _Y_synthetic if _time == `earthquake_start' - `b'
	gen comp_post_earthquake_`b' = _Y_synthetic if _time == `earthquake_end' + `b'
}

capture noisily drop _Co_Number _W_Weight _Y_treated _Y_synthetic iso_code2 _time
capture noisily collapse growth_earthquake comp_earthquake growth_pre_earthquake_1 growth_post_earthquake_1 comp_pre_earthquake_1 comp_post_earthquake_1 growth_pre_earthquake_2 growth_post_earthquake_2 comp_pre_earthquake_2 comp_post_earthquake_2 growth_pre_earthquake_3 growth_post_earthquake_3 comp_pre_earthquake_3 comp_post_earthquake_3 growth_pre_earthquake_4 growth_post_earthquake_4 comp_pre_earthquake_4 comp_post_earthquake_4 growth_pre_earthquake_5 growth_post_earthquake_5 comp_pre_earthquake_5 comp_post_earthquake_5, by(iso_code earthquake_start earthquake_end)
capture noisily order iso_code earthquake_start earthquake_end growth_pre_earthquake_5 growth_pre_earthquake_4 growth_pre_earthquake_3 growth_pre_earthquake_2 growth_pre_earthquake_1 growth_earthquake growth_post_earthquake_1 growth_post_earthquake_2 growth_post_earthquake_3 growth_post_earthquake_4 growth_post_earthquake_5 comp_pre_earthquake_5 comp_pre_earthquake_4 comp_pre_earthquake_3 comp_pre_earthquake_2 comp_pre_earthquake_1 comp_earthquake comp_post_earthquake_1 comp_post_earthquake_2 comp_post_earthquake_3 comp_post_earthquake_4 comp_post_earthquake_5
save "$root\Data\Working data\Synth\earthquake_`var'_processed_`i'.dta", replace
}
restore
}

clear
forvalues i = 1(1)`number'{
capture noisily append using "$root\Data\Working data\Synth\earthquake_`var'_processed_`i'.dta"
}
drop if iso_code == "VNM" & earthquake_start == 1970
save "$root\Data\Working data\earthquake_`var'_dataset.dta", replace
}